package com.naihe.anderson.sms.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.adapter.GalleryImageAdapter;
import com.naihe.anderson.sms.application.NhSmsApp;
import com.naihe.anderson.sms.constants.NhSmsConstants;
import com.naihe.anderson.sms.util.FileUtil;
import com.naihe.anderson.sms.widgets.ColorPickerDialog;
import com.naihe.anderson.sms.widgets.GalleryFlow;

public class GalleryActivity extends Activity implements OnClickListener, OnItemClickListener, OnItemLongClickListener {
	Button back_btn;
	GalleryFlow galleryFlow;
	
	GalleryImageAdapter imageAdapter;
	NhSmsApp nhSmsApp;
	List<String> list = new ArrayList<String>();
	RelativeLayout gallery_layout;
	
	public static final int MSG_WHAT_DATA_CHANGE = 0;
	Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_WHAT_DATA_CHANGE:
				getFile();
				break;
			default:
				break;
			}
		}
		
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_layout);
		nhSmsApp = (NhSmsApp) getApplication();
		nhSmsApp.setActivity(this);
		initView();
		
		mHandler.sendEmptyMessageDelayed(MSG_WHAT_DATA_CHANGE, 100);
	}
	
	public void getFile(){
		List<String> tempList = new ArrayList<String>();
		String path = FileUtil.getPicNotePath();
		if(!TextUtils.isEmpty(path)){
			File filePath = new File(path);
			File[] files = filePath.listFiles();
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if(file.isFile() && file.getName().startsWith(NhSmsConstants.SCREENSHOT_PREFIX)){
					tempList.add(file.getName());
				}
			}
		} else {
			Toast.makeText(GalleryActivity.this, "无SD卡", Toast.LENGTH_SHORT).show();
		}
		list.addAll(tempList);
		
		if(list.size() > 0){
			imageAdapter = new GalleryImageAdapter(GalleryActivity.this, list);
			galleryFlow.setAdapter(imageAdapter);
			galleryFlow.setOnItemClickListener(this);
		}
	}

	public void initView() {
		gallery_layout = (RelativeLayout) findViewById(R.id.gallery_layout);
		gallery_layout.setBackgroundColor(nhSmsApp.getDefaultSharedPreferences().getInt("GALLERY_BG_COLOR", 0));
		galleryFlow = (GalleryFlow) this.findViewById(R.id.gallery);
		galleryFlow.setFadingEdgeLength(0);
		galleryFlow.setSpacing(-50); //图片之间的间距
		galleryFlow.setOnItemClickListener(this);
		
		back_btn = (Button) findViewById(R.id.back_btn);
		back_btn.setOnClickListener(this);
	}

	private void toFinishActivity(){
		nhSmsApp.setActivity(null);
		finish();
		System.gc();
	}
	
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.back_btn) {
			toFinishActivity();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 截获按键事件
		if (KeyEvent.KEYCODE_BACK == keyCode) {
			toFinishActivity();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
	}


	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		
		return false;
	}
	
	/**
     * 建立菜单item
     * 
     * */
	public static final int LOGINTOUT = Menu.FIRST + 0;
    public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0,LOGINTOUT,0,getResources().getString(R.string.color_choic)).setIcon(R.drawable.color_choice);
		
		return true;
	}

    /**
     * 响应菜单点击事件
     * 
     * */
    @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()	== LOGINTOUT){
			ColorPickerDialog dialog = new ColorPickerDialog(GalleryActivity.this, 0,  getResources().getString(R.string.color_choic),   
                    new ColorPickerDialog.OnColorChangedListener() {
				
		                @Override  
		                public void colorChanged(int color) {  
		                	nhSmsApp.getDefaultSharedPreferences().edit().putInt("GALLERY_BG_COLOR", color).commit();
		                	gallery_layout.setBackgroundColor(color);
		                }  
            	});  
            dialog.show();
		}
		return super.onOptionsItemSelected(item);
	}
}
